【例子介绍】CamelinAction(2ndEd)
【相关图片】
【源码结构】
brief contents
Part 1 First steps.....................................................................1
1 ■ Meeting Camel 3
2 ■ Routing with Camel 27
Part 2 Core Camel.................................................................73
3 ■ Transforming data with Camel 75
4 ■ Using beans with Camel 106
5 ■ Enterprise integration patterns 146
6 ■ Using components 194
Part 3 Developing and testing .......................................... 239
7 ■ Microservices 241
8 ■ Developing Camel projects 306
9 ■ Testing 343
10 ■ RESTful web services 408
Part 4 Going further with Camel .................................... 467
11 ■ Error handling 469
12 ■ Transactions and idempotency 514
13 ■ Parallel processing 562
14 ■ Securing Camel 595
Licensed to nick sun <cwh.xpx@qq.com>
viii brief contents
Part 5 Running and managing Camel ................................ 623
15 ■ Running and deploying Camel 625
16 ■ Management and monitoring 671
Part 6 Out in the wild ........................................................ 715
17 ■ Clustering 717
18 ■ Microservices with Docker and Kubernetes 752
19 ■ Camel tooling 803
Bonus chapters ......
Available at https://www.manning.com/books/camel-in-action-second-edition
and in electronic versions of this book
20 ■ Reactive Camel
21 ■ Camel and the IoT by Henryk Konsek
Licensed to nick sun <cwh.xpx@qq.com>
ix
contents
foreword by James Strachan xxiii
foreword by Dr. Mark Little xxv
foreword to the first edition xxvii
preface xxix
acknowledgments xxxi
about this book xxxiii
about the authors xxxvii
about the cover illustration xxxix
Part 1 First steps ......................................................1
1
Meeting Camel 3
1.1 Introducing Camel 4
What is Camel? 4
■ Why use Camel? 5
1.2 Getting started 10
Getting Camel 10
■ Your first Camel ride 11 ■ Camel’s
message model 15
■ Message 15 ■ Exchange 16
1.4 Camel’s architecture 18
Architecture from 10,000 feet 18
■ Camel concepts 19
1.5 Your first Camel ride, revisited 24
1.6 Summary 25
Licensed to nick sun <cwh.xpx@qq.com>
x x contents
2
Routing with Camel 27
2.1 Introducing Rider Auto Parts 28
2.2 Understanding endpoints 29
Consuming from an FTP endpoint 30
■ Sending to a JMS
endpoint 31
2.3 Creating routes in Java 33
Using RouteBuilder 34
■ Using the Java DSL 35
2.4 Defining routes in XML 39
Bean injection and Spring 40
■ The XML DSL 43 ■ Using
Camel and Spring 46
2.5 Endpoints revisited 49
Sending to dynamic endpoints 49
■ Using property placeholders
in endpoint URIs 50
■ Using raw values in endpoint
URIs 54
■ Referencing registry beans in endpoint URIs 54
2.6 Routing and EIPs 55
Using a content-based router 55
■ Using message
filters 61
■ Using multicasting 62 ■ Using recipient
lists 66
■ Using the wireTap method 69
2.7 Summary and best practices 71
Part 2 Core Camel ..................................................73
3
Transforming data with Camel 75
3.1 Data transformation overview 76
3.2 Transforming data by using EIPs and Java 77
Using the Message Translator EIP 78
■ Using the
Content Enricher EIP 84
3.3 Transforming XML 87
Transforming XML with XSLT 87
■ Transforming XML
with object marshaling 88
3.4 Transforming with data formats 91
Data formats provided with Camel 92
■ Using Camel’s CSV data
format 93
■ Using Camel’s Bindy data format 94 ■ Using
Camel’s JSON data format 97
■ Configuring Camel data
formats 98
3.5 Transforming with templates 99
Using Apache Velocity 99
Licensed to nick sun <cwh.xpx@qq.com>
xi contents
3.6 Understanding Camel type converters 100
How the Camel type-converter mechanism works 101
Using Camel type converters 102
■ Writing your own
type converter 103
3.7 Summary and best practices 105
4
Using beans with Camel 106
4.1 Using beans the hard way and the easy way 107
Invoking a bean from pure Java 108
■ Invoking a bean defined in
XML DSL 108
■ Using beans the easy way 109
4.2 Understanding the Service Activator pattern 111
4.3 Using Camel’s bean registries 113
JndiRegistry 114
■ SimpleRegistry 115 ■
ApplicationContextRegistry 116
■ OsgiServiceRegistry and
BlueprintContainerRegistry 116
■ CdiBeanRegistry 117
4.4 Selecting bean methods 119
How Camel selects bean methods 120
■ Camel’s
method- selection algorithm 121
■ Some method-
selection examples 123
■ Potential method- selection
problems 125
■ Method selection using type matching 127
4.5 Performing bean parameter binding 128
Binding with multiple parameters 129
■ Binding using built- in
types 130
■ Binding using Camel annotations 131 ■ Binding
using Camel language annotations 132
■ Parameter binding
using method name with signature 136
4.6 Using beans as predicates and expressions 138
Using beans as predicates in routes 139
■ Using beans as
expressions in routes 141
4.7 Summary and best practices 144
5
Enterprise integration patterns 146
5.1 Introducing enterprise integration patterns 146
The Aggregator and Splitter EIPs 147
■ The Routing Slip and
Dynamic Router EIPs 148
■ The Load Balancer EIP 148
5.2 The Aggregator EIP 148
Using the Aggregator EIP 149
■ Completion conditions
for the Aggregator 152
■ Using persistence with the
Aggregator 159
■ Using recovery with the Aggregator 163
Licensed to nick sun <cwh.xpx@qq.com>
xii contents
5.3 The Splitter EIP 167
Using the Splitter 167
■ Using beans for splitting 170
Splitting big messages 172
■ Aggregating split messages 174
When errors occur during splitting 176
5.4 The Routing Slip EIP 178
Using the Routing Slip EIP 179
■ Using a bean to compute the
routing slip header 179
■ Using an Expression as the routing
slip 180
■ Using @RoutingSlip annotation 180
5.5 The Dynamic Router EIP 182
Using the Dynamic Router 182
■ Using the @DynamicRouter
annotation 183
5.6 The Load Balancer EIP 184
Introducing the Load Balancer EIP 184
■ Using load- balancing
strategies 186
■ Using the failover load balancer 188 ■ Using a
custom load balancer 190
5.7 Summary and best practices 192
6
Using components 194
6.1 Overview of Camel components 195
Manually adding components 196
■ Autodiscovering
components 196
6.2 Working with files: File and FTP components 199
Reading and writing files with the File component 200
■ Accessing
remote files with the FTP component 203
6.3 Asynchronous messaging: JMS component 204
Sending and receiving messages 208
■ Request- reply
messaging 209
■ Message mappings 210
6.4 Networking: Netty4 component 213
Using Netty for network programming 214
■ Using custom
codecs 216
6.5 Working with databases: JDBC and JPA components 218
Accessing data with the JDBC component 218
■ Persisting objects
with the JPA component 221
6.6 In- memory messaging: Direct, Direct- VM, SEDA, and VM
components 226
Synchronous messaging with Direct and Direct-
VM 227
■ Asynchronous messaging with SEDA and VM 228
Licensed to nick sun <cwh.xpx@qq.com>
xiii contents
6.7 Automating tasks: Scheduler and Quartz2 components 230
Using the Scheduler component 230
■ Enterprise
scheduling with Quartz 231
6.8 Working with email 233
Sending mail with SMTP 234
■ Receiving mail with IMAP 235
6.9 Summary and best practices 236
Part 3 Developing and testing ...........................239
7
Microservices 241
7.1 Microservices overview 242
Small in size 242
■ Observable 243 ■ Designed for
failure 243
■ Highly configurable 244 ■ Smart endpoints and
dumb pipes 244
■ Testable 245
7.2 Running Camel microservices 245
Standalone Camel as microservice 245
■ CDI Camel
as microservice 247
■ WildFly Swarm with Camel as
microservice 252
■ Spring Boot with Camel as microservice 256
7.3 Calling other microservices 262
Recommendation prototype 264
■ Shopping cart
prototype 266
■ Rules and inventory prototypes 270 ■ Rating
prototype 273
■ Putting all the microservices together 275
7.4 Designing for failures 277
Using the Retry pattern to handle failures 277
■ Using the
Retry pattern without Camel 279
■ Using Circuit Breaker to
handle failures 280
■ Netflix Hystrix 282 ■ Using Hystrix
with Camel 284
■ Configuring Hystrix 287 ■ Bulkhead
pattern 289
■ Calling other microservices with fault-
tolerance 292
■ Using Camel Hystrix with Spring
Boot 298
■ The Hystrix dashboard 301
7.5 Summary and best practices 304
8
Developing Camel projects 306
8.1 Managing projects with Maven 307
Using Camel Maven archetypes 307
■ Using Maven to add Camel
dependencies 311
8.2 Using Camel in Eclipse 313
Creating a new Camel project 313
Licensed to nick sun <cwh.xpx@qq.com>
xiv contents
8.3 Debugging an issue with your new Camel project 316
8.4 Developing custom components 318
Setting up a new Camel component 318
■ Diving into the
implementation 320
8.5 Generating components with the API component
framework 326
Generating the skeleton API project 326
■ Configuring the
camel- api- component- maven- plugin 328
■ Setting advanced
configuration options 331
■ Implementing remaining
functionality 335
8.6 Developing data formats 338
Generating the skeleton data format project 338
■ Writing the
custom data format 339
8.7 Summary and best practices 341
9
Testing 343
9.1 Introducing the Camel Test Kit 345
Using the Camel JUnit extensions 346
■ Using camel- test to test
Java Camel routes 346
■ Unit testing an existing RouteBuilder
class 349
9.2 Testing Camel with Spring, OSGi, and CDI 349
Camel testing with Spring XML 350
■ Camel testing with Spring
Java Config 352
■ Camel testing with Spring Boot 356 ■ Camel
testing with OSGi Blueprint XML 358
■ Camel testing with
CDI 361
■ Camel testing with WildFly Swarm 363 ■ Camel
testing with WildFly 364
9.3 Using the mock component 368
Introducing the mock component 369
■ Unit- testing with
the mock component 369
■ Verifying that the correct message
arrives 371
■ Using expressions with mocks 372 ■ Using mocks
to simulate real components 376
9.4 Simulating errors 378
Simulating errors using a processor 378
■ Simulating errors using
mocks 380
■ Simulating errors using interceptors 381 ■ Using
adviceWith to add interceptors to an existing route 382
■ Using
adviceWith to manipulate routes for testing 383
■ Using weave
with adviceWith to amend routes 387
9.5 Camel integration testing 390
Performing integration testing 391
■ Using NotifyBuilder 393
Licensed to nick sun <cwh.xpx@qq.com>
xv contents
9.6 Using third- party testing libraries 395
Using Arquillian to test Camel applications 395
■ Using Pax
Exam to test Camel applications 399
■ Using other testing
libraries 404
9.7 Summary and best practices 405
10
RESTful web services 408
10.1 RESTful services 409
Understanding the principles of a RESTful API 409
■ Using
JAX- RS with REST services 410
■ Using Camel in an existing
JAX- RS application 414
■ Using camel- restlet with REST
services 417
■ Using camel- cxf with REST services 420
10.2 The Camel Rest DSL 425
Exploring a quick example of the Rest DSL 426
■ Understanding
how the Rest DSL works 427
■ Using supported components for the
Rest DSL 429
■ Configuring Rest DSL 429 ■ Using XML and
JSON data formats with Rest DSL 434
■ Calling RESTful services
using Rest DSL 443
10.3 API documentation using Swagger 444
Using Swagger with JAX- RS REST services 446
■ Using
Swagger with Rest DSL 451
■ Documenting Rest DSL
services 452
■ Documenting input, output, and error
codes 454
■ Configuring API documentation 459 ■ Using
CORS and the Swagger web console 461
10.4 Summary and best practices 465
Part 4 Going further with Camel .....................467
11
Error handling 469
11.1 Understanding error handling 470
Recoverable and irrecoverable errors 470
■ Where Camel’s
error handling applies 473
11.2 Using error handlers in Camel 473
Using the default error handler 474
■ The dead letter channel error
handler 475
■ The transaction error handler 480 ■ The no
error handler 480
■ The logging error handler 481 ■ Features of
the error handlers 481
11.3 Using error handlers with redelivery 481
An error- handling use case 482
■ Using redelivery 482 ■ Using
DefaultErrorHandler with redelivery 484
■ Error handlers and
scopes 487
■ Reusing context- scoped error handlers 490
Licensed to nick sun <cwh.xpx@qq.com>
xvi contents
11.4 Using exception policies 492
Understanding how onException catches exceptions 492
Understanding how onException works with redelivery 496
Understanding how onException can handle exceptions 497
Custom exception handling 500
■ New exception while handling
exception 502
■ Ignoring exceptions 503 ■ Implementing
an error- handler solution 504
■ Bridging the consumer with
Camel’s error handler 505
11.5 Working with other error- handling features 509
Using onWhen 509
■ Using onExceptionOccurred 510 ■ Using
onRedeliver 511
■ Using retryWhile 512
11.6 Summary and best practices 512
12
Transactions and idempotency 514
12.1 Why use transactions? 515
The Rider Auto Parts partner integration application 516
Setting up the JMS broker and the database 518
■ The story of the
lost message 520
■ Transaction basics 521 ■ Understanding
Spring’s transaction support 522
■ Adding transactions 524
Testing transactions 526
12.2 Transaction basics 521
Understanding Spring’s transaction support 522
■ Adding
transactions 524
■ Testing transactions 526
12.3 The Transactional Client EIP 529
Using local transactions 530
■ Using global
transactions 531
■ Transaction starting from a database
resource 534
■ Transaction redeliveries 536 ■ Using different
transaction propagations 539
■ Returning a custom response
when a transaction fails 542
12.4 Compensating for unsupported transactions 544
Introducing UnitOfWork 545
■ Using synchronization
callbacks 546
■ Using onCompletion 548
12.5 Idempotency 551
Idempotent Consumer EIP 552
■ Idempotent
repositories 555
■ Clustered idempotent repository 556
12.6 Summary and best practices 560
Licensed to nick sun <cwh.xpx@qq.com>
xvii contents
13
Parallel processing 562
13.1 Introducing concurrency 563
Running the example without concurrency 565
■ Using
concurrency 565
13.2 Using thread pools 571
Understanding thread pools in Java 571
■ Using Camel thread
pool profiles 573
■ Creating custom thread pools 575 ■ Using
ExecutorServiceManager 576
13.3 Parallel processing with EIPs 578
Using concurrency with the Threads EIP 579
■ Using concurrency
with the Multicast EIP 581
■ Using concurrency with the Wire
Tap EIP 583
13.4 Using the asynchronous routing engine 586
Hitting the scalability limit 586
■ Scalability in
Camel 588
■ Components supporting asynchronous
processing 589
■ Asynchronous API 590 ■ Writing a custom
asynchronous component 591
■ Potential issues when using an
asynchronous component 593
13.5 Summary and best practices 594
14
Securing Camel 595
14.1 Securing your configuration 596
Encrypting configuration 596
■ Decrypting configuration 597
14.2 Web service security 599
Authentication in web services 600
■ Authenticating web services
using JAAS 604
14.3 Payload security 608
Digital signatures 608
■ Payload encryption 612
14.4 Transport security 614
Defining global SSL configuration 617
14.5 Route authentication and authorization 618
Configuring Spring Security 619
14.6 Summary and best practices 621
Licensed to nick sun <cwh.xpx@qq.com>
xviii contents
Part 5 Running and managing Camel .................623
15
Running and deploying Camel 625
15.1 Starting Camel 626
How Camel starts 626
■ Camel startup options 629 ■ Ordering
routes 631
■ Disabling autostartup 634
15.2 Starting and stopping routes at runtime 635
Using CamelContext to start and stop routes at runtime 635
Using the Control Bus EIP to start and stop routes at runtime 638
Using RoutePolicy to start and stop routes at runtime 639
15.3 Shutting down Camel 641
Graceful shutdown 642
15.4 Deploying Camel 645
Embedded in a Java application 645
■ Embedded in a web
application 648
■ Embedded in WildFly 654
15.5 Camel and OSGi 658
Setting up Maven to generate an OSGi bundle 659
■ Installing
and running Apache Karaf 660
■ Using an OSGi Blueprint-based
Camel route 661
■ Deploying the example 661 ■ Using a
managed service factory to spin up route instances 663
15.6 Camel and CDI 666
15.7 Summary and best practices 670
16
Management and monitoring 671
16.1 Monitoring Camel 672
Checking health at the network level 672
■ Checking health level at
the JVM level 675
■ Checking health at the application level 675
16.2 Using JMX with Camel 676
Using JConsole to manage Camel 676
■ Using JConsole to remotely
manage Camel 678
■ Using Jolokia to manage Camel 680
16.3 Tracking application activity 684
Using log files 685
■ Using core logs 685 ■ Using custom
logging 685
■ Using Tracer 689 ■ Using notifications 691
16.4 Managing Camel applications 694
Managing Camel application lifecycles 694
■ Using Jolokia and
hawtio to manage Camel lifecycles 695
■ Using Control Bus to
manage Camel 697
Licensed to nick sun <cwh.xpx@qq.com>
xix contents
16.5 The Camel management API 698
Accessing the Camel management API using Java 699
■ Using
Camel management API from within Camel 703
■ Performance
statistics 705
■ Management- enable custom Camel
components 708
■ Management- enable custom Java beans 711
16.6 Summary and best practices 713
Part 6 Out in the wild .........................................715
17
Clustering 717
17.1 Clustered HTTP 718
17.2 Clustered Camel routes 720
Active/passive mode 720
■ Active/active mode 720 ■ Clustered
active/passive mode using Hazelcast 721
■ Clustered active/
passive mode using Consul 723
■ Clustered active/passive mode
using ZooKeeper 724
17.3 Clustered JMS 726
Client- side clustering with JMS and ActiveMQ 726
17.4 Clustered Kafka 727
Kafka consumer offset 730
■ Crashing a JVM with a running
Kafka consumer 731
17.5 Clustering caches 733
Clustered cache using Hazelcast 733
■ Clustered cache using
JCache and Infinispan 736
17.6 Using clustered scheduling 737
Clustered scheduling using Quartz 737
17.7 Calling clustered services using the Service Call EIP 739
How the Service Call EIP works 740
■ Service Call using static
service registry 741
■ Service Call with failover 744 ■ Configuring
Service Call EIP 745
■ Service Call URI templating 747
Service Call using Spring Boot Cloud and Consul 748
17.8 Summary and best practices 750
18
Microservices with Docker and Kubernetes 752
18.1 Getting started with Camel on Docker 753
Building and running Camel microservices locally 754
Building and running Camel microservices using Docker 756
Building a Docker image using the Docker Maven plugin 758
Running Java microservices on Docker 759
Licensed to nick sun <cwh.xpx@qq.com>
xx contents
18.2 Getting started with Kubernetes 762
Installing Minikube 762
■ Starting Minikube 763
18.3 Running Camel and other applications in Kubernetes 764
Running applications using kubectl 765
■ Calling a service
running inside a Kubernetes cluster 766
■ Running Java
applications in Kubernetes using Maven tooling 768
■ Java
microservices calling each other in the cluster 771
■ Debugging
Java applications in Kubernetes 774
18.4 Understanding Kubernetes 776
Introducing Kubernetes 776
■ Kubernetes
architecture 778
■ Essential Kubernetes concepts 778
18.5 Building resilient Camel microservices on Kubernetes 783
Scaling up microservices 784
■ Using readiness and liveness
probes 786
■ Dealing with failures by calling services in
Kubernetes 789
18.6 Testing Camel microservices on Kubernetes 792
Setting up Arquillian Cube 792
■ Writing a basic unit test
using Arquillian Cube 793
■ Running Arquillian Cube tests
on Kubernetes 793
■ Writing a unit test that calls a Kubernetes
service 794
18.7 Introducing fabric8, Helm, and OpenShift 796
fabric8 796
■ Kubernetes Helm 796 ■ OpenShift 798
18.8 Summary and best practices 800
19
Camel tooling 803
19.1 Camel editors 804
JBoss Fuse Tooling 804
■ Apache Camel IDEA
plugin 808
■ Camel validation using Maven 811
19.2 Camel Catalog: the information goldmine 813
19.3 hawtio: a web console for Camel and Java applications 816
Understanding hawtio functionality 817
■ Debugging Camel
routes using hawtio 818
19.4 Summary and best practices 821
appendix A Simple, the expression language 823
appendix B The Camel community 835
index 839
Licensed to nick sun <cwh.xpx@qq.com>
xxi contents
Bonus chapters ......
Available at https://www.manning.com/books/camel-in-action-second-edition
and in electronic versions of this book
20
Reactive Camel 1
20.1 Using Reactive Streams with Camel 2
Reactive Streams API 2
■ Reactive flow control with back
pressure 2
■ First steps with Reactive Streams 4 ■ Using
Camel with Reactive Streams 6
■ Controlling back pressure from
the producer side 9
■ Controlling back pressure from the consumer
side 14
20.2 Using Vert.x with Camel 16
Building a football simulator using Vert.x 16
■ Using Camel
together with Vert.x 22
■ Summary of using Camel with Vert.x for
microservices 25
20.3 Summary and best practices 25
21
Camel and the IoT 1
21.1 The Internet of Things shopping list 2
Raspberry Pi 3
■ SD card for Raspberry Pi 4 ■ Power bank
for Raspberry Pi 4
■ Camera for Raspberry Pi 4 ■ TI
SensorTag 4
21.2 The Internet of Things architecture 5
21.3 Why Camel is the right choice for the IoT 6
Components 7
■ Data formats 7 ■ Redelivery 7
Throttling 7
■ Content- based routing 8 ■ Client- side load
balancing 8
■ Control bus 8
21.4 Gateway- to- data- center connectivity 9
Understanding the architecture 9
■ Choosing a protocol 10
21.5 Camel and Eclipse Kura 13
Starting Kura in emulator mode 14
■ Defining Camel routes
using the Kura web UI 15
■ Next steps with Camel and Kura 17
21.6 Next steps with Camel and the IoT 17
21.7 Summary 18
评论